#!/bin/bash
BASE_PATH=$(cd $(dirname $0);pwd )
jdk_local_package=$(find $(pwd) -maxdepth 1 -size +100M -type f -iname "jdk*" | head -1)

echo "本地已有安装包:${jdk_local_package[@]}"


jdk_version_list=($( curl -s https://repo.huaweicloud.com/java/jdk/ | egrep -o 'href="[0-9][^/]*' | grep -v demo | sed "s@href=\"@@g;s/\n//g" ))

if [ 0 != ${#jdk_version_list[@]} ];then
    for i in $(seq 1 ${#jdk_version_list[@]})
    do
        printf "[%s] %-25s\n" ${i} ${jdk_version_list[$i-1]}
    done
else
    echo "get version list faild."
    exit 1
fi

read -p "请输入安装版本的序号，默认: $(expr ${#jdk_version_list[@]} - 1)。输入:" num

if [ -n "$num" ];then
    #echo "正则后:${num//[0-9]}"
    if [   -z "${num//[0-9]}" ];then
        #echo "num:${num};\njdk_version_list:${#jdk_version_list[@]};"
        if [[ ${num} -lt 1 || ${#jdk_version_list[@]} -lt ${num} ]];then
            echo "输入的数字介于1-${#jdk_version_list[@]}"
            exit 1
        fi
    else
        echo "${num}不是纯数字"
        exit 1
    fi
else
    num=$(expr ${#jdk_version_list[@]} - 1)
fi

jdk_version="${jdk_version_list[$num-1]}"
echo "jdk_version:${jdk_version}"


#echo "curl -s https://repo.huaweicloud.com/java/jdk/${jdk_version}/ | egrep -o '<a href=\"[0-9][^/]*' | sed 's@<a\shref=\"@@g;s/\n//g'"
jdk_filename_list=($(curl -s https://repo.huaweicloud.com/java/jdk/${jdk_version}/ | egrep -o '<a href="jdk[^"]*' | grep -i 'linux' | grep '64' | grep -v "arm" | sed "s@<a\shref=\"@@g;s/\n//g"))

if [ 0 != ${#jdk_filename_list[@]} ];then
    for i in $(seq 1 ${#jdk_filename_list[@]})
    do
        printf "[%s] %-25s\n" ${i} ${jdk_filename_list[$i-1]}
    done 
fi

read -p "请输入下载安装包的序号，默认: tar.gz。输入:" fnum


if [ -n "${fnum}" ];then
    #echo "正则后:${fnum//[0-9]}"
    if [   -z "${fnum//[0-9]}" ];then
        #echo "fnum:${fnum};\njdk_filename_list:${#jdk_filename_list[@]};"
        if [[ ${fnum} -lt 1 || ${#jdk_filename_list[@]} -lt ${fnum} ]];then
            echo "输入的数字介于1-${#jdk_filename_list[@]}"
            exit 1
        fi
    else
        echo "${fnum}不是纯数字"
        exit 1
    fi
else
    for i in $(seq 1 ${#jdk_filename_list[@]})
    do
        printf "[%s] %-25s\n" ${i} ${jdk_filename_list[$i-1]}
        echo ${jdk_filename_list[$i-1]//.tar.gz}
    done 
    fnum=2
fi

jdk_filename="${jdk_filename_list[$fnum-1]}"
if [ ! -e ${jdk_filename} ];then
    echo "自 https://repo.huaweicloud.com/java 下载安装包文件:"
    curl -#O https://repo.huaweicloud.com/java/jdk/${jdk_version}/${jdk_filename}
fi
echo "jdk_filename:${jdk_filename}"
#echo "curl -#O https://repo.huaweicloud.com/java/jdk/${jdk_version}/${jdk_filename} "

if [[ "${jdk_filename}" =~ .*.tar.gz ]];then
    install_dir=/opt
    echo "文件类型: tar.gz"
    total_size=$(gzip -l ${jdk_filename} | awk 'NR == 2 {print $2}' )
    block_size=`expr $total_size / 51200`
    block_size=`expr $block_size + 1`
    tar --blocking-factor=$block_size --checkpoint=1 --checkpoint-action="ttyout=解压至:${install_dir}/\t进度:%u%    \r" -zxf ${jdk_filename} -C ${install_dir}/ &&\
    printf "\n"
    java_home=${install_dir}/$(  tar -ztf ${jdk_filename} | head -1 | sed 's#/.*##' )
    sed '/.*JAVA_HOME.*/d' -i /etc/profile
echo "
export JAVA_HOME=${java_home}
export JRE_HOME=\${JAVA_HOME}/jre
export CLASSPATH=.:\${JAVA_HOME}/lib/dt.jar:\${JAVA_HOME}/lib/tools.jar
export PATH=\${JAVA_HOME}/bin:\$PATH" >> /etc/profile
    source /etc/profile
    echo "JAVA_HOME: ${JAVA_HOME}"

elif [[ "${jdk_filename}" =~ .*.rpm ]];then
    echo "文件类型: rpm"
    echo "rpm -Uvh ${jdk_filename}"
fi